package com.soft.mainsite.module.web.system;


import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.module.web.cms.service.ContentService;
import com.soft.mainsite.module.web.system.service.OrganizationService;
import com.soft.mainsite.module.web.system.service.RoleService;
import com.soft.mainsite.module.web.system.service.SysUserService;
import com.soft.mainsite.module.web.system.vo.UserVo;
import com.soft.mainsite.mybatis.model.TSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


/**
 * Description:用户管理
 *
 * @author Jin
 * @create 2017-04-06
 **/
@Controller
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private OrganizationService orgService;

    @Autowired
    private ContentService contentService;

    @RequestMapping
    public ModelAndView index() {
        return new ModelAndView("system/admin");
    }

    /**
     * 后台用户列表
     *
     * @param pageNumber
     * @param pageSize
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(
            @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize,
            UserVo user, HttpServletRequest request) {
        UserVo userVo = ((UserVo) request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY));
        ModelAndView view = new ModelAndView("system/admin_list");
        user.setUserId(userVo.getUserId());
        view.addObject("model", sysUserService.findSysUserPageInfo(pageNumber, pageSize, user, false));
        view.addObject("user", user);
        return view;

    }

    /**
     * 后台用户修改
     *
     * @param userId
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/input")
    public String input(@RequestParam(value = "userId", required = false) Integer userId, Model model, HttpServletRequest request) {
        if (userId != null) {
            model.addAttribute("user", sysUserService.findSysUserByUserId(userId));
            model.addAttribute("userRole", roleService.findByUserId(userId));
        }
        UserVo userVo = ((UserVo) request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY));
        model.addAttribute("myRoleList", roleService.findByUserId(userVo.getUserId()));
        model.addAttribute("orgList", orgService.findByPid(0));
        model.addAttribute("roleList", roleService.findAll());
        return "system/admin_input";
    }

    /**
     * 后台用户更新
     *
     * @param user
     * @param request
     * @param roleIds
     * @param orgIds
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(TSysUser user, HttpServletRequest request,
                         @RequestParam(value = "roleId", required = false) Integer[] roleIds,
                         @RequestParam(value = "orgId", required = false) String orgIds
    ) {
        if (user.getUserId() != null) {
            return sysUserService.update(user, roleIds, orgIds);
        }
        UserVo userVo = ((UserVo) request.getSession().getAttribute(CmsConst.SITE_USER_SESSION_KEY));
        return sysUserService.save(user, roleIds, orgIds, userVo);
    }


    /**
     * 后台用户删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("userId") Integer[] id) {
        return sysUserService.delete(id);
    }


    /**
     * 校验用户名是否重复
     *
     * @param name
     * @return
     */
    @RequestMapping("/valid/username")
    @ResponseBody
    public String validSysUserName(@RequestParam("username") String name) {
        if (sysUserService.findSysUserByUsername(name) != null) {
            return "{\"error\": \"名字已经被占用啦\"}";
        }
        return "{\"ok\": \"名字很棒\"}";
    }


}
